package com.zsyh.jiujiu.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.example.zyh.toolslib.utils.SPUtils;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.MyInfoBeeean;
import com.zsyh.jiujiu.net.MyFragementObserver;
import com.zsyh.jiujiu.net.MyObserver;
import com.zsyh.jiujiu.utils.StatusUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class TingxingDialog extends DialogFragment implements ClallBackData {

    @BindView(R.id.tishi)
    TextView tishi;
    @BindView(R.id.quxiao)
    TextView quxiao;
    @BindView(R.id.queding)
    TextView queding;
    @BindView(R.id.shengji)
    TextView shengji;
    private Unbinder unbinder;
    private ClallBackData clallBackData;
    String num;
    private String cishu;

    public TingxingDialog(String num) {
        this.num = num;
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_tixing, container, false);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        unbinder = ButterKnife.bind(this, view);
        gettUser((String) SPUtils.get(getActivity(), "userid", ""));


        return view;
    }

    public void setSelectAddresFinish(ClallBackData clallBackData) {
        this.clallBackData = clallBackData;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setLayout(dm.widthPixels, dm.heightPixels - StatusUtil.getStatusBarHeight(getActivity()));
    }


    @Override
    public void getData(String data, String name, String number) {

    }

    @OnClick({R.id.quxiao, R.id.queding,R.id.shengji})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.quxiao:
                clallBackData.getData("0", "", "");
                dismiss();
                break;
            case R.id.queding:
                clallBackData.getData("1", "", "");
                dismiss();
                break;

            case R.id.shengji:
                clallBackData.getData("3", "", "");
                dismiss();
                break;
        }
    }

    /**
     * getuser
     */
    public void gettUser(String userid) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getUserById(userid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(), false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("Myinfo", JsonStr + "");
                        Gson gson = new Gson();
                        MyInfoBeeean myInfoBeeean = gson.fromJson(JsonStr, MyInfoBeeean.class);
                        cishu=myInfoBeeean.getData().getViewTimes()+"";
                        if (num.equals("1")) {
                            tishi.setText("您还没有发布过商品\n请您先发布商品");
                            queding.setText("发布商品");
                        } else if (num.equals("2")) {
                            tishi.setText("只有星级会员才能与该产品\n发布用户在线聊天");
                            queding.setText("升级会员");
                            quxiao.setText("联系客服");
                        } else if (num.equals("3")) {
                            tishi.setText("您还可以联系对方"+cishu+"次,请继续使用");
                            queding.setText("确定");
                            quxiao.setText("取消");
                            shengji.setText("升级会员");
                            shengji.setVisibility(View.VISIBLE);
                        }else if (num.equals("4")) {
                            tishi.setText("您还可以联系对方"+cishu+"次,请继续使用");
                            queding.setText("确定");;
                            quxiao.setText("取消");
                        }
                    }
                });
    }
}
